import 'package:intl/intl.dart';
import 'package:wch/utils/log_utils.dart';

class TimeUtils {
  ///获取当前时间戳
  static int getCurrentTimeStamp([String? time]) {
    if (time == null) {
      return DateTime.now().millisecondsSinceEpoch ~/ 1000;
    }
    return DateTime.parse(time).millisecondsSinceEpoch ~/ 1000;
  }
  ///获取当前时间戳,毫秒
  static int getCurrentMillTimeStamp([String? time]) {
    if (time == null) {
      return DateTime.now().millisecondsSinceEpoch;
    }
    return DateTime.parse(time).millisecondsSinceEpoch;
  }
  ///获取当前时间
  static String getCurTime([String split = '']) {
    DateTime dateTime = DateTime.now();
    int year = dateTime.year;
    int month = dateTime.month;
    int day = dateTime.day;
    int hour = dateTime.hour;
    int minute = dateTime.minute;
    int second = dateTime.second;

    String MM = month < 10 ? '0$month' : '$month';
    String dd = day < 10 ? '0$day' : '$day';
    String hh = hour < 10 ? '0$hour' : '$hour';
    String mm = minute < 10 ? '0$minute' : '$minute';
    String ss = second < 10 ? '0$second' : '$second';

    return '$year$split$MM$split$dd$split$hh:$mm:$ss';
  }
  static String convertChinaFormatDate(String date) {
    List<String> dates = date.split('-');
    if (dates.length == 3) {
      return '${dates.first}年${dates[1]}月${dates[2]}日';
    }
    return getChinaFormatCurDate();
  }
  ///获取当前时间，中文时间格式
  static String getChinaFormatCurDate() {
    DateTime dateTime = DateTime.now();
    int year = dateTime.year;
    int month = dateTime.month;
    int day = dateTime.day;
    int hour = dateTime.hour;
    int minute = dateTime.minute;

    String MM = month < 10 ? '0$month' : '$month';
    String dd = day < 10 ? '0$day' : '$day';
    String hh = hour < 10 ? '0$hour' : '$hour';
    String mm = minute < 10 ? '0$minute' : '$minute';

    return '$year年$MM月$dd日';
  }
  ///获取当前时间，中文时间格式
  static String getChinaFormatCurTime() {
    DateTime dateTime = DateTime.now();
    int year = dateTime.year;
    int month = dateTime.month;
    int day = dateTime.day;
    int hour = dateTime.hour;
    int minute = dateTime.minute;
    int second = dateTime.second;

    String MM = month < 10 ? '0$month' : '$month';
    String dd = day < 10 ? '0$day' : '$day';
    String hh = hour < 10 ? '0$hour' : '$hour';
    String mm = minute < 10 ? '0$minute' : '$minute';

    return '$year年$MM月$dd日 $hh:$mm';
  }
  ///获取当前日期
  static String getCurDate([String split = '']) {
    DateTime dateTime = DateTime.now();
    int year = dateTime.year;
    int month = dateTime.month;
    int day = dateTime.day;

    String MM = month < 10 ? '0$month' : '$month';
    String dd = day < 10 ? '0$day' : '$day';

    return '$year$split$MM$split$dd';
  }
  ///获取当前时分
  static String getCurHourMinute() {
    DateTime dateTime = DateTime.now();
    int hour = dateTime.hour;
    int minute = dateTime.minute;

    String hh = hour < 10 ? '0$hour' : '$hour';
    String mm = minute < 10 ? '0$minute' : '$minute';

    return '$hh:$mm';
  }
  ///秒数转换为 时分秒
  ///showType 显示类型：0-分秒，1-时分，2-时分秒
  ///alwaysHour 是否需要显示小时，1 一直显示，0 大于等于1小时才显示
  ///showType = 2时，alwaysHour 才会起作用
  static String second2HHmmss(int second, int showType, {alwaysHour = int} ) {
    if (second < 0) {
      return '';
    }

    int minute = second % 3600 ~/ 60;
    int sec = second % 60;
    int hour = second ~/ 3600;

    String hourStr = '';
    String minuteStr = '';
    String secondStr = '';

    if (hour >= 10) {
      hourStr = '$hour';
    } else if(hour < 1) {
      hourStr = '00';
    } else {
      hourStr = '0$hour';
    }

    if (minute >= 10) {
      minuteStr = '$minute';
    } else if (minute < 10) {
      minuteStr = '0$minute';
    }

    if (sec >= 10) {
      secondStr = '$sec';
    } else {
      secondStr = '0$sec';
    }

    if (showType == 0) {
      return '$minuteStr:$secondStr';
    } else if (showType == 1){
      return '$hourStr:$minuteStr';
    } else {
      if (alwaysHour == 1) {
        return '$hourStr:$minuteStr:$secondStr';
      } else {
        if (hour > 0) {
          return '$hourStr:$minuteStr:$secondStr';
        } else {
          return '$minuteStr:$secondStr';
        }
      }
    }
  }
  ///显示的格式为：0时04分08秒
  static String second2ChinaHHmmss(int second) {
    int s = second % 60;
    String ss = s >= 10 ? '$s' : '0$s';

    int m = second % 3600 ~/ 60;
    String mm = m >= 10 ? '$m' : '0$m';

    int h = second ~/ 3600;

    return '$h时$mm分$ss秒';
  }

  static String timeStampToTimeStr(int timestamp) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
    // 定义时间格式
    DateFormat dateFormat = DateFormat('yyyy-MM-dd HH:mm:ss.SSS');
    // 格式化时间
    String formattedTime = dateFormat.format(dateTime);
    return formattedTime;
  }

  static int calculateAge(String birth) {
    LogUtils.log('birth=$birth');
    if (birth.contains('*')) {
      return 0;
    }
    DateTime birthDate = DateTime.parse(birth);
    DateTime currentDate = DateTime.now();
    int age = currentDate.year - birthDate.year;
    int month1 = currentDate.month;
    int month2 = birthDate.month;
    if (month2 > month1) {
      age--;
    } else if (month1 == month2) {
      int day1 = currentDate.day;
      int day2 = birthDate.day;
      if (day2 > day1) {
        age--;
      }
    }
    return age;
  }
}